package com.copa.l104;

/**
 * @author copa
 * @createDate 2022-07-15 12:45
 * @function 递归法
 */
public class Test1 {

    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        return findDepth(root, 1);
    }

    private int findDepth(TreeNode root, int depth) {
        int temp = depth;
        int left = 0, right = 0;
        if (root.left != null) {
            left = findDepth(root.left, temp + 1);
        }
        if (root.right != null) {
            right = findDepth(root.right, temp + 1);
        }
        return Math.max(temp, Math.max(left, right));
    }

    public static void main(String[] args) {
        TreeNode left = new TreeNode(9);
        TreeNode right = new TreeNode(20);
        right.left = new TreeNode(15);
        right.right = new TreeNode(7);
        TreeNode root = new TreeNode(3, left, right);
        System.out.println(new Test1().maxDepth(root));
    }
}
